<?php
namespace backend\controllers;

use Yii;
use backend\controllers\BaseController;
use yii\data\Pagination;
use common\components\CreateExcel;
use backend\models\course\Course;
use backend\models\course\CourseOrder;
use backend\models\course\CourseOrderInfo;
/**
 * 订单功能模块
 * @author 王文秀
 */
class OrderController extends BaseController
{
	/**
	 * @inheritdoc  课程订单管理
	 */
	public function actionIndex()
	{
		$session = Yii::$app->session;
		if(!empty(Yii::$app->request->get('page'))){
			$session->open();
			$order=$session->get('order');
			$cond=$session->get('cond');
		}else{
		$order = Yii::$app->request->post();
		//搜索条件
		$cond = ['and'];
		if(!empty($order)){
		if(!empty($order['startDateTime']) && !empty($order['endDateTime'])) $cond[] = ['between', 'order_addtime', strtotime($order['startDateTime']), strtotime($order['endDateTime'])];
		if(!empty($order['orderStatus'])) $cond[] = ['=','order_status',$order['orderStatus']];
		if($order['orderStatus'] == "0") $cond[] = ['=','order_status',$order['orderStatus']];
		if(!empty($order['payType'])) $cond[] = ['=','pay_type',$order['payType']];
		if($order['keywordType'] == 'username' && !empty($order['keyword'])){
			$cond[] = ['=','user_name',$order['keyword']];
		}else if($order['keywordType']=='ordersn'){
			$cond[] = ['=','order_sn',$order['keyword']];
		}
		$session->open();
		$session->set('order', $order);
		$session->set('cond', $cond);
		//print_r($order);die;
		}else{
		$order['startDateTime']='';
		$order['endDateTime']='';
		 $order['orderStatus']='';
		 $order['payType']='';
		 $order['keywordType']='';
		 $order['keyword']='';

		
		}
		}
		//print_r($cond);die;
		$model = new CourseOrder;
		$pagination = new Pagination([
			'defaultPageSize' => 5,
			'totalCount' => $model->OrderCount($cond),
		]);
		$data = $model->OrderSearch($cond,$pagination);
		return $this->render('index',[
						'data' => $data,
						'model' => $model,
						'pagination' => $pagination,
						'order' => $order,
		]);
	}
	/**
	 * @inheritdoc  课程订单详情
	 */
	public function actionOrderinfo()
	{
		$request = \YII::$app->request;
		$order_id = $request->post("order_id");
		$model = new CourseOrder;
		$data = $model->Order($order_id);
		foreach ($data['courseorderinfo'] as $kk => $vv) {
			$course_id[] = $vv['course_id'];
		}
		if (!empty($course_id)) {
			$course_ids = implode(",", $course_id);
			$Course = new Course;
			$CourseData = $Course->CourseSel($course_ids);
			foreach ($CourseData as $courseval) {
				$CourseName[] = $courseval['course_name'];
			}
			if (!empty($CourseName)) {
				$CourseNames = implode(",", $CourseName);
			}
			$data['course_name']=$CourseNames;
		}
		return $this->renderPartial('orderinfo',[
						'order' => $data,
		]);
	}
	
	/**
	 * @inheritdoc  导出订单列表
	 */
	public function actionExport()
	{
		$order = \YII::$app->request->get();
		//搜索条件
		$cond = ['and'];
		if(!empty($order['startDateTime']) && !empty($order['endDateTime'])) $cond[] = ['between', 'order_addtime', strtotime($order['startDateTime']), strtotime($order['endDateTime'])];
		if(!empty($order['orderStatus'])) $cond[] = ['=','order_status',$order['orderStatus']];
		if($order['orderStatus'] == "0") $cond[] = ['=','order_status',$order['orderStatus']];
		if(!empty($order['payType'])) $cond[] = ['=','pay_type',$order['payType']];
		if($order['keywordType'] == 'username' && !empty($order['keyword'])){
			$cond[] = ['=','user_name',$order['keyword']];
		}else if($order['keywordType']=='ordersn'){
			$cond[] = ['=','order_sn',$order['keyword']];
		}
		$model = new CourseOrder;
		$pagination = new Pagination([
			'defaultPageSize' => 5,
			'totalCount' => $model->OrderCount($cond),
		]);
		$data = $model->OrderSearch($cond,$pagination);
		foreach($data as $k=>$v){
			$arr[$k]['order_sn']=$v['order_sn'];
			$arr[$k]['order_addtime']=date("Y-m-d H:i:s",$v['order_addtime']);
			if($v['order_status']== "0"){
				$arr[$k]['order_status'] = "未支付";
			}elseif($v['order_status']== "1"){
				$arr[$k]['order_status'] = "已付款";
			}elseif($v['order_status']== "2"){
				$arr[$k]['order_status'] = "订单关闭";
			}elseif($v['order_status']== "3"){
				$arr[$k]['order_status'] = "成功";
			}
			$arr[$k]['price_amount']=$v['price_amount'];
			$arr[$k]['user_name']=$v['user_name'];
			if($v['pay_type']== "0"){
				$arr[$k]['pay_type'] = " ";
			}elseif($v['pay_type']== "1"){
				$arr[$k]['pay_type'] = "支付宝";
			}elseif($v['pay_type']== "2"){
				$arr[$k]['pay_type'] = "微信支付";
			}elseif($v['pay_type']== "3"){
				$arr[$k]['pay_type'] = "快捷支付";
			}
			$arr[$k]['pay_time']=date("Y-m-d H:i:s",$v['pay_time']);
		}
		if (!$arr){
            echo "<script>alert('您要导出的数据不存在！');location.href='/index.php?r=order/index'</script>";die;
        }

        //生成excel
        $excel = new CreateExcel();
        $excel->createByArray($arr,'订单结果列表',['订单号','创建时间','状态','实付（元）','购买者','支付方式','支付时间']);
	}
}
